package common;

public class Vector {
	public Vector() {
		X = 0;
		Y = 0;
	}
	public Vector(float x, float y) {
		X = x;
		Y = y;
	}
	public float X;
	public float Y;
	
	public void normalize() {
		float d = (float)Math.sqrt((X * X) + (Y * Y));
		X = X / d;
		Y = Y / d;
	}
	public boolean isNormalized() {
		float sLength = (X * X) + (Y * Y);
		if(sLength >= 0.999f && sLength <= 1.001) {
			return true;
		} else {
			return false;
		}
	}
	public float length() {
		return (float)Math.sqrt((X * X) + (Y * Y));
	}
	
	public void scale(float factor) {
		X = X * factor;
		Y = Y * factor;
	}
	public void rotate(float radians) {
		float x, y;
		x = (float)((X * Math.cos(radians) - (Y * Math.sin(radians)))); 
		y = (float)((Y * Math.cos(radians) + (X * Math.sin(radians))));
		X = x;
		Y = y;
	}
	public void add(Vector vector) {
		X += vector.X;
		Y += vector.Y;
	}
	public void sub(Vector vector) {
		X -= vector.X;
		Y -= vector.Y;
	}
	public float dot(Vector other) {
		float dotProduct = (X * other.X) + (Y * other.Y); 
		if(other.isNormalized() && this.isNormalized()) {
			return dotProduct;
		} else {
			return dotProduct / (this.length() * other.length());
		}
	}
	
	public Vector copyTo(Vector other) {
		if(other == null) {
			return new Vector(X, Y);
		}
		other.X = X;
		other.Y = Y;
		return other;
	}
	public Vector clone() {
		return new Vector(X, Y);
	}
	
	@Override
	public String toString() {
		return "{X:" + X + " Y:" + Y + " L:" + length() + "}";
	}
}
